package function;

import View.menu;

import java.sql.*;
import java.util.InputMismatchException;
import java.util.Scanner;

public class local {
    public static void choiceLogin_ch(String choice){
        ResultSet resultSet=null;
        PreparedStatement preparedStatement=null;
        try {
            Connection connection = DriverManager.getConnection(con.URL,con.user,con.password);
                PreparedStatement preparedStatement1=null;
                ResultSet resultSet1=null;
                Scanner sc=new Scanner(System.in);
                switch (choice) {
                    case "1": {
                        //登陆系统
                        System.out.print("请输入用户名：");
                        String name = sc.next();
                        System.out.print("请输入密码：");
                        String passwd = sc.next();

                        String query = "SELECT * FROM User where username = ? AND password = ?";
                        preparedStatement = connection.prepareStatement(query);
                        preparedStatement.setString(1, name);
                        preparedStatement.setString(2, passwd);
                        resultSet = preparedStatement.executeQuery();
                        if (resultSet.next()) {
                            System.out.println("登陆成功！");
                            System.out.println("欢迎你， "+name);
                            menu.menu1_ch();
                        } else {
                            System.out.println("密码或用户名错误");
                        }
                        break;
                    }
                    case "2": {

                        int id=0;
                        boolean gold=true;
                        while(gold) {
                            try {
                                System.out.println("请输入ID ");
                                id = sc.nextInt();
                                String quary= "select *from User where id = ?";
                                preparedStatement=connection.prepareStatement(quary);
                                preparedStatement.setInt(1,id);
                                resultSet=preparedStatement.executeQuery();
                                if(resultSet.next()){
                                    System.out.println("ID 已存在");
                                }
                                else {
                                    gold = false;
                                }
                            } catch (InputMismatchException e) {
                                System.out.println("ID的数据类型错误");
                                sc.nextLine();
                            }
                        }
                    /*
                        为了防止用户输入错误，使用while循环和try-catch语句来进行修正
                     */

                        System.out.print("请输入用户名：");
                        String Nname=sc.next();
                        System.out.print("请输入密码：");
                        String Npasswd=sc.next();
                        //检查逻辑
                        String query = "select * from User where id=?";
                        preparedStatement = connection.prepareStatement(query);
                        preparedStatement.setInt(1, id);
                        resultSet = preparedStatement.executeQuery();//查询结果进入resultset
                        if(resultSet.next()) {
                            System.out.println("用户已存在");
                        }
                        else{
                            //写入逻辑
                            String ins = "insert into User (id,username,password) values (?, ?, ?)";
                            preparedStatement = connection.prepareStatement(ins);
                            preparedStatement.setInt(1,id);
                            preparedStatement.setString(2, Nname);
                            preparedStatement.setString(3, Npasswd);
                            preparedStatement.executeUpdate();//插入语句
                        }
                        break;
                    }
                    case "3": {
                        //退出逻辑
                        System.out.println("下次见！");
                        break;
                    }
                    default:{
                        System.out.println("请输入正确的选项");
                    }
                }

        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
